package com.qf.service.impl;

import com.qf.entity.MdItemType;
import com.qf.entity.MdItemTypeVO;
import com.qf.entity.SysUser;
import com.qf.mapper.MdItemTypeMapper;
import com.qf.service.MdItemTypeServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * --- Be Humble and Hungry ---
 *
 * @author McMeloxD
 * @date 2024/7/18
 * @desc
 */
@Service
public class MdItemTypeServiveImpl implements MdItemTypeServive {

    @Autowired
    private MdItemTypeMapper mdItemTypeMapper;

    @Override
    public List<MdItemTypeVO> findAllItemType(String itemTypeName, String enableFlag) {
        return mdItemTypeMapper.findAllItemType(itemTypeName,enableFlag);
    }

    @Override
    public List<MdItemType> hasChildren(int id) {
        return mdItemTypeMapper.hasChildren(id);
    }

    @Override
    public int delType(int id) {
        return mdItemTypeMapper.delType(id);
    }

    @Override
    public boolean addType(MdItemType itemType) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes( )).getRequest( );
        // 取出登录者的名称,setter进对象里
        SysUser info = (SysUser)request.getSession().getAttribute("userInfo");
        itemType.setCreateBy(info.getUsername());
        //获取创建时间，setter进去
        itemType.setCreateTime(new Date());
        //获取最新的type code
        String code = createNewCode();
        itemType.setItemTypeCode(code);
        //再获取它的族谱,直接将传来的父级typeid拼接到从库里查到父级的族谱后面就行
        String ancestors = mdItemTypeMapper.getFqAncestors(itemType.getParentTypeId());
        String newAncestors = ancestors + "," + itemType.getParentTypeId();
        itemType.setAncestors(newAncestors);
        System.out.println("最终itemType为"+itemType);
        return mdItemTypeMapper.addType(itemType);
    }

    @Override
    public boolean updateType(MdItemType itemType) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes( )).getRequest( );
        // 取出登录者的名称,setter进对象更新者里
        SysUser info = (SysUser)request.getSession().getAttribute("userInfo");
        itemType.setUpdateBy(info.getUsername());
        //更新时间，setter进去
        itemType.setUpdateTime(new Date());
        return mdItemTypeMapper.updateType(itemType);
    }

    @Override
    public String getFqName(int id) {
        return mdItemTypeMapper.getFqName(id);
    }

    private String createNewCode(){
        String itemTypeCode = mdItemTypeMapper.findNewCode();
        //先按_拆分字符串，取最后的一部分就是数字部分
        String[] arr = itemTypeCode.split("_");
        int codeNum = Integer.parseInt(arr[arr.length - 1]);
        codeNum++;//先自增
        int temp = codeNum; //存一下temp，方便一会计算位数
        int n = 0; //比较用
        //遍历获取位数
        while (temp % 10 != 0){
            temp = temp / 10; // 如果还有余数就整除10
            n++;
        }

        //将code按剩余位数拼接0
        String code = "ITEM_TYPE_";
        while(4 - n > 0){
            code += "0";
            n++;
        }
        //最终code
        code = code+codeNum;
        return code;
    }
}
